package com.ruoyi.app.controller;

import com.ruoyi.app.domain.ProductInfo;
import com.ruoyi.app.domain.vo.ProductInfoVo;
import com.ruoyi.app.enums.ProductInfoTypeEnum;
import com.ruoyi.app.service.IProductInfoService;
import com.ruoyi.app.service.IShopInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 商品Controller
 * 
 * @author zkq
 * @date 2024-07-28
 */
@ApiOperation(value = "商品信息", tags = "商品信息")
@RestController
@RequestMapping("/restock/productInfo")
public class RestockProductInfoController extends BaseController
{
    @Autowired
    private IProductInfoService productInfoService;

    /**
     * 查询商品列表
     */
//    @PreAuthorize("@ss.hasPermi('product:productInfo:list')")
    @GetMapping("/list")
    public AjaxResult list(ProductInfo productInfo)
    {
        startPage();
        productInfo.setShopId(getShopId());
        productInfo.setType(ProductInfoTypeEnum.RESTOCK.getType());
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return  success(getDataTable(list));
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('product:productInfo:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductInfo productInfo)
    {
        productInfo.setShopId(getShopId());
        productInfo.setType(ProductInfoTypeEnum.RESTOCK.getType());
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
//    @PreAuthorize("@ss.hasPermi('product:productInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(productInfoService.selectProductInfoById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('product:productInfo:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.save(productInfo));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('product:productInfo:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.updateById(productInfo));
    }


    /**
     * 修改商品
     */
//    @PreAuthorize("@ss.hasPermi('product:productInfo:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody ProductInfo productInfo)
    {
        productInfo.setShopId(getShopId());
        productInfo.setType(ProductInfoTypeEnum.RESTOCK.getType());
        return AjaxResult.success().setData(productInfoService.addOrUpdate(productInfo));
    }

    /**
     * 添加商品数量
     * @param productInfo
     * @return
     */
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/addProductNumber")
    public AjaxResult addProductNumber(@RequestBody ProductInfo productInfo)
    {
        productInfo.setShopId(getShopId());
        return AjaxResult.success().setData(productInfoService.addProductNumber(productInfo));
    }

    /**
     * 删除商品
     */
//    @PreAuthorize("@ss.hasPermi('product:productInfo:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        List<String> objects = Arrays.asList(ids);
        return toAjax(productInfoService.removeByIds(objects));
    }

    /**
     * 销售统计
     */
//    @PreAuthorize("@ss.hasPermi('product:productInfo:remove')")
//    @Log(title = "商品", businessType = BusinessType.DELETE)
	@GetMapping("/salesStatistics")
    public AjaxResult remove(@RequestBody ProductInfoVo ids)
    {
        return toAjax(productInfoService.salesStatistics(ids));
    }
}
